//
//  ZCSingerAlbumTableViewCell.m
//  VoiceMusic
//
//  Created by dllo on 15/10/24.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCSingerAlbumTableViewCell.h"
#import "ZCSingerAlbumCollectionViewCell.h"
#import <Masonry.h>
#import "ZCAlbumSingerViewController.h"
@implementation ZCSingerAlbumTableViewCell

- (void)dealloc
{
    self.collection.delegate = nil;
    self.collection.dataSource = nil;
}

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatView];
    }
    return self;
}

- (void)creatView
{
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.minimumInteritemSpacing = 5;
    
    _layout.itemSize = CGSizeMake((self.contentView.frame.size.width - 40) / 2, self.contentView.frame.size.height);
    //竖向之间的距离
    _layout.minimumLineSpacing = 0;
    self.layout.sectionInset = UIEdgeInsetsMake(5, 10, 10, 10);
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
   
    [self.collection setDataSource:self];
    [self.collection setDelegate:self];
    self.collection.backgroundColor = [UIColor blackColor];
    [self.collection registerClass:[ZCSingerAlbumCollectionViewCell class] forCellWithReuseIdentifier:@"m"];
    self.collection.scrollEnabled = NO;
    [self.collection setBackgroundColor:[UIColor whiteColor]];
    
    [self.contentView addSubview:self.collection];
}

- (void)layoutSubviews
{
    [super superview];
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    _layout.itemSize = CGSizeMake(170 / 375.0 * self.contentView.frame.size.width, 170 / 667.0 * self.contentView.frame.size.height);
}

#pragma mark collection协议
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.mainArray count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZCSingerAlbumCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"m" forIndexPath:indexPath];
    
    if (self.mainArray.count > 0) {
        ZCSingerAlbumModel * model = [self.mainArray objectAtIndex:indexPath.row];
        [cell getInfo:model];
    }
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZCSingerAlbumModel * model = [self.mainArray objectAtIndex:indexPath.row];
    NSString *string = [NSString stringWithFormat:@"%@", model.albumId];
    NSLog(@"%@", string);
    NSDictionary *dic = @{@"albumId":string};
   [[NSNotificationCenter defaultCenter] postNotificationName:@"tagValue" object:nil userInfo:dic];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
